#region Copyright (c) 2006 Jonathan Nauman
/*       _\|/_
         (o o)
+-----oOO-{_}-OOo-----------------------+
| Copyright (c) Jonathan Naumann        |
+---------------------------------------+
| 
| File: MonoPad.InterfaceLogic.cs
| Version:	0.1.7
+
*/
#endregion

//#define __DEBUG

using System;
using System.Collections.Generic;
using Gtk;
using GtkSourceView;
using Glade;
using System.IO;

namespace MonoPad {
	///
	/// Class Interface Logic
	///
	public class InterfaceLogic {
		///
		/// Default constructor
		///
		public InterfaceLogic() {
		}
		
		///
		/// Get the mimetype of a file
		///
		public string GetMimetype(string extension) {
			string mime = "";
			
			switch(extension) {
				case ".cs":
					mime = "text/x-csharp";
					break;
				
				case ".cpp":
				case ".cc":
				case ".h":
					mime = "text/x-cpp";
					break;
				
				case ".c":
					mime = "text/x-c";
					break;
				
				case ".xml":
					mime = "text/xml";
					break;
				
				case ".css":
					mime = "text/css";
					break;
				
				case ".js":
					mime = "text/x-javascript";
					break;
				
				case ".boo":
					mime = "text/x-boo";
					break;
				
				case ".vb":
					mime = "text/x-vb";
					break;
				
				case ".java":
					mime = "text/x-java";
					break;
				
				case ".sh":
					mime = "text/x-sh";
					break;
				
				case ".sql":
					mime = "text/x-sql";
					break;
				
				case ".d":
					mime = "text/x-dsrc";
					break;
				
				case ".perl":
					mime = "text/x-perl";
					break;
				
				case ".php":
					mime = "text/x-php";
					break;
				
				case ".python":
					mime = "text/x-python";
					break;
				
				case ".html":
				case ".htm":
				case ".shtml":
					mime = "text/html";
					break;
				
				default:
					mime = "text/plain";
					break;
			}
			
			return mime;
		}
		
		///
		/// Action method for the open buttons
		///
		public string ActionOpen(ref FileContainer oFC, ref Window window) {
			#if __DEBUG
				Console.WriteLine("MonoPad -> Open FileOpen dialog ...");
			#endif
			
			string mime = "";
			
			FileChooserDialog oFCD = new FileChooserDialog("Open file ...", null, FileChooserAction.Open);
			
			try {
				oFCD.SetCurrentFolderUri(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
			}
			catch(Exception) {
				oFCD.SetCurrentFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
			}
			
			oFCD.AddButton(Stock.Cancel, ResponseType.Cancel);
			oFCD.AddButton(Stock.Open, ResponseType.Ok);
			oFCD.DefaultResponse = ResponseType.Ok;
			oFCD.WindowPosition = Gtk.WindowPosition.Center;
			
			if(oFCD.Run() == (int)ResponseType.Ok) {
				oFC.Filename = oFCD.Uri;
				oFC.Filename = oFC.Filename.Replace("file://", "");
				
				#if __DEBUG
					Console.WriteLine("Open file: " + oFC.Filename);
				#endif
				
				string Exp = System.IO.Path.GetExtension(oFC.Filename);
				
				oFC.Filestream = File.OpenRead(oFC.Filename);
				mime = this.GetMimetype(Exp);
			}
			else {
				oFC.Filename = "";
				oFC.Filestream = null;
			}
			
			oFCD.Destroy();
			
			return mime;
		}
		
		///
		/// Action method for the save buttons
		///
		public void ActionSave(string Buffer, string Filename) {
			if(Buffer == "") return;
			if(Filename == "") return;
			
			#if __DEBUG
				Console.WriteLine("MonoPad -> Open Save dialog ...");
			#endif
			
			FileHandler oFH = new FileHandler(Filename);
			oFH.SaveFile(Buffer);
			
			#if __DEBUG
				Console.WriteLine("File " + Filename + " saved ...");
			#endif
		}
		
		///
		/// Action method for the save as buttons
		///
		public void ActionSaveAs(string strBuffer, ref FileContainer oFC) {
			#if __DEBUG
				Console.WriteLine("MonoPad -> Open SaveAs dialog ...");
			#endif
			
			FileChooserDialog oFCD = new FileChooserDialog("Save file as ...", null, FileChooserAction.Save);
			
			try {
				oFCD.SetCurrentFolderUri(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
			}
			catch(Exception) {
				oFCD.SetCurrentFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
			}
			
			oFCD.AddButton(Stock.Cancel, ResponseType.Cancel);
			oFCD.AddButton(Stock.SaveAs, ResponseType.Ok);
			oFCD.DefaultResponse = ResponseType.Ok;
			oFCD.WindowPosition = Gtk.WindowPosition.Center;
			
			if(oFCD.Run() == (int)ResponseType.Ok) {
				oFC.Filename = oFCD.Uri;
				
				oFC.Filename = oFC.Filename.Replace("file://", "");
				
				FileHandler oFH = new FileHandler(oFC.Filename);
				oFH.SaveFile(strBuffer);
				
				#if __DEBUG
					Console.WriteLine("File " + oFC.Filename + " saved as " + " ... ");
				#endif
			}
			
			oFCD.Destroy();
		}
		
		///
		/// Action method for the new buttons
		///
		public void ActionNew(Notebook noteTab, ConfigContainer config) {
			GtkSourceView.SourceView oTemp = new SourceView();
			
			if(config.LineNumbers) {
				oTemp.ShowLineNumbers = true;
			}
			
			ScrolledWindow sw = new ScrolledWindow();
			sw.AddWithViewport(oTemp);
			
			noteTab.AppendPage(sw, new Label("NewFile.cs"));
			
			noteTab.ShowAll();
			noteTab.NextPage();
			
			#if __DEBUG
				Console.WriteLine("Added a new tab (now " + noteTab.NPages + ") ...");
			#endif
		}
		
		///
		/// Action method for the new buttons
		///
		public void ActionNew(Notebook noteTab, ConfigContainer config, ref List<GtkSourceView.SourceView> views) {
			GtkSourceView.SourceView oTemp = new SourceView();
			
			if(config.LineNumbers) {
				oTemp.ShowLineNumbers = true;
			}
			
			ScrolledWindow sw = new ScrolledWindow();
			sw.AddWithViewport(oTemp);
			
			views.Add(new GtkSourceView.SourceView());
			
			noteTab.AppendPage(sw, new Label("NewFile.cs"));
			
			noteTab.ShowAll();
			noteTab.NextPage();
			
			#if __DEBUG
				Console.WriteLine("Added a new tab (now " + noteTab.NPages + ") ...");
			#endif
		}
		
		///
		/// Action method for closing tab action
		///
		public int ActionCloseTab(ref Window window, string buffer, string newbuffer, ref Notebook noteTab) {
			int cp = noteTab.CurrentPage;
			
			if(buffer == "" || buffer != newbuffer) {
				noteTab.RemovePage(cp);
				return cp;
			}
			else {
				MessageDialog md = new MessageDialog(
					window, 
					DialogFlags.DestroyWithParent,
					MessageType.Question,
					ButtonsType.YesNo,
					"Are you sure you want to close the tab?"
				);
				
				ResponseType result = (ResponseType)md.Run();
				
				if(result == ResponseType.Yes) {
					noteTab.RemovePage(cp);
					md.Destroy();
					
					return cp;
				}
				else {
					md.Destroy();
					
					return -1;
				}
			}
		}
		
		///
		/// Action method for the exit buttons
		///
		public void ActionExit(ref Window window, string buffer, string newbuffer) {
			if(buffer == "" || buffer != newbuffer) {
				Gtk.Application.Quit();
				
				#if __DEBUG
					Console.WriteLine("Close MonoPad ...");
				#endif
			}
			else {
				MessageDialog md = new MessageDialog(
					window, 
					DialogFlags.DestroyWithParent,
					MessageType.Question,
					ButtonsType.YesNo,
					"Are you sure you want to quit?"
				);
				
				ResponseType result = (ResponseType)md.Run();
				
				if(result == ResponseType.Yes) {
					Gtk.Application.Quit();
					
					#if __DEBUG
						Console.WriteLine("Close MonoPad ...");
					#endif
				}
				else {
					md.Destroy();
				}
			}
		}
	}
	
}
